# 分析结果数据-项目类
# by lw 2023-11-04

import json
from package import Package


class Project():

    def __init__(self,
                 projectId: int = 0,
                 indexId: int = 0,
                 linkurl: str = "",
                 region: str = "",
                 projectNum: str = "",
                 projectName: str = "",
                 totalAmount: float = 0.00,
                 punit: str = "",
                 punitAddress: str = "",
                 publishTime: str = ""):
        """构造函数

        Args:
            projectId (int, optional): 项目标识码. Defaults to 0.
            indexId (int, optional): 索引标识码. Defaults to 0.
            linkurl (str, optional): 来源url. Defaults to "".
            region (str, optional): 区域（省份）. Defaults to "".
            projectNum (str, optional): 项目编号. Defaults to "".
            projectName (str, optional): 项目名称. Defaults to "".
            totalAmount (float, optional): 项目总金额. Defaults to 0.00.
            punit (str, optional): 采购单位. Defaults to "".
            punitAddress (str, optional): 采购单位地址. Defaults to "".
            publishTime (str, optional): （中标）发布日期. Defaults to "".
        """
        self.projectId = projectId  # 项目标识码
        self.indexId = indexId  # 索引标识码
        self.linkurl = linkurl  # 来源url
        self.region = region  # 区域（省份）
        self.projectNum = projectNum  # 项目编号
        self.projectName = projectName  # 项目名称
        self.totalAmount = totalAmount  # 项目总金额
        self.punit = punit  # 采购单位
        self.punitAddress = punitAddress  # 采购单位地址
        self.publishTime = publishTime  # （中标）发布日期
        self.packages = []  # 项目包

    def add_package(self, p: Package) -> object:
        """向项目中添加一个包信息

        Args:
            p (Package): 项目包信息

        Returns:
            object: 项目对象self
        """
        p.projectId = self.projectId
        p.packageId = len(self.packages) + 1
        self.totalAmount += p.amount
        self.packages.append(p)
        return self

    def tostring(self) -> str:
        """将对象序列化为json字符串格式

        Returns:
            str: json字符串
        """
        return json.dumps(
            self,
            default=lambda obj: obj.__dict__,
            #   sort_keys=True,
            indent=2,
            ensure_ascii=False)

    def from_json(self, d: json) -> object:
        """对象反序列化:从json对象反序列化

        Args:
            d (json): Project类对象对应的json对象

        Returns:
            object: Project类对象self
        """
        self.projectId = d['projectId']
        self.indexId = d['indexId']
        self.linkurl = d['linkurl']
        self.region = d['region']
        self.projectNum = d['projectNum']
        self.projectName = d['projectName']
        self.totalAmount = d['totalAmount']
        self.punit = d['punit']
        self.punitAddress = d['punitAddress']
        self.publishTime = d['publishTime']
        return self

    def from_string(self, jstring: str) -> object:
        """Project类对象反序列化：将描述的对象json字符串反序列化对象

        Args:
            jstring (str): 描述对象的json字符串

        Returns:
            object: Project类对象self
        """
        return json.loads(jstring, object_hook=self.from_json)


# 测试代码：
if __name__ == '__main__':
    print("__name__:", __name__)
    # 不同的实例化：
    d = Project()
    print("无参数实例化的对象tostring:", d.tostring())
    d = Project(projectName="测试项目")
    print("有参数实例化的对象tostring:", d.tostring())
    # 反序列化
    j = json.loads(d.tostring())
    print("Json Object of Project:", j)
    d2 = Project()
    d2.from_json(j)
    print("使用json对象实例化的对象tostring:", d2.tostring())
    d3 = Project()
    d3.from_string(d.tostring())
    print("使用json字符串实例化的对象tostring:", d3.tostring())
    # 加入2个包：
    p = Package(cunit="中标单位1", amount=1000.00)
    d3.add_package(p)
    p = Package(cunit="中标单位2", amount=10000.00)
    d3.add_package(p)
    print("有两个包的Project:", d3.tostring())
